import IntegrationTable, {
	IntegrationTableProps,
	OperatingItem,
} from "@/components/IntegrationTable";
import { ProColumns } from "@ant-design/pro-table";
import React, { useEffect, useState } from "react";
import {
	Button,
	message,
	Popconfirm,
	Space,
	Image,
	Cascader,
	Tag,
	Divider,
	Descriptions,
} from "antd";
import Search from "antd/lib/input/Search";
import { user, classify, remove, userList } from "./service";
import { arrayToObject, getArray } from "phinney-toolkit";
import { DynamicFormItem } from "@/components/DynamicForm";
import axios from "axios";
// @page 商家
const Business: React.FC<any> = () => {
	// 组件是否已经卸载
	let isUnMounted = false;
	// 用户列表
	let [followList, setFollowList] = useState<any>([]);

	const getFollowList = async () => {
		const res = await userList({});
		!isUnMounted && setFollowList(getArray(res?.data));
	};

	// 表格项
	const columns: ProColumns[] = [
		{
			title: "id",
			dataIndex: "id",
			search: false,
		},
		{
			title: "渠道",
			dataIndex: "user",
			hideInTable: true,
			renderFormItem: (
				item,
				{ type, defaultRender, record, ...rest },
				filterForm
			) => {
				return (
					<div>
						<Cascader
							options={followList}
							fieldNames={{
								label: "user",
								value: "user",
								children: "list",
							}}
							changeOnSelect
							onChange={async (value: any, selectedOptions: any) => {
								filterForm.setFieldsValue({
									user: value.join(","),
								});
							}}
							placeholder="请选择~"
						/>
					</div>
				);
			},
		},
		{
			title: "名称",
			dataIndex: "name",
			search: false,
		},
		{
			title: "头像",
			dataIndex: "avatar",
			search: false,
			render: (_, record: any) => {
				if (record.avatar) {
					return <Image src={record.avatar} width={50} alt="" />;
				} else {
					return <div>-</div>;
				}
			},
		},

		{
			title: "备注",
			dataIndex: "followUserRemark",
			search: false,
		},
		{
			title: "渠道名称",
			dataIndex: "followUserState",
			search: false,
		},
		{
			title: "渠道用户ID",
			dataIndex: "followUserUserid",
			search: false,
		},
		{
			title: "创建时间",
			dataIndex: "createTime",
			search: false,
		},
	];

	// 表单项
	const getFormItems = (vm: any, isEdit?: boolean): DynamicFormItem[] => {
		const { coupon } = vm.state;
		return [
			{
				type: "upload",
				label: "分类图片",
				name: "classifyIcon",
				fieldProps: {
					limit: 1,
				},
				required: true,
			},
			{
				type: "text",
				label: "分类名称",
				name: "classifyName",
				required: true,
			},
			{
				type: "custom",
				label: "上级分类",
				name: "parentId",
				render: (form: any, record: any) => {
					return (
						<div>
							<Cascader
								defaultValue={[record.parentName] || ""}
								options={vm.state.classsifyList}
								fieldNames={{
									label: "classifyName",
									value: "id",
									children: "list",
								}}
								changeOnSelect
								onChange={(item) => {
									form.setFieldsValue({
										parentId: item[item.length - 1],
									});
								}}
								placeholder="请选择类别~"
							/>
						</div>
					);
				},
			},
		];
	};

	// 一体化表格props
	const tablePorps: IntegrationTableProps = {
		rowKey: "classifyCode",
		scroll: { x: 1500 },
		columns,
		listProps: {
			url: "/wechat/friend-list",
			method: "GET",
			requestType: "params",
			paramsHandle: (params: any) => {
				return {
					...params,
				};
			},
		},
	};

	// 初始化加载数据
	useEffect(() => {
		getFollowList();
		return () => {
			isUnMounted = true;
		};
	}, []);

	return <IntegrationTable {...tablePorps} />;
};

export default Business;
